package 力扣_树算法.二叉树;

import java.util.ArrayDeque;
import java.util.Deque;

/**104. 二叉树的最大深度
 * @author zx
 * @create 2022-04-10 21:09
 */
public class    Num104 {
    /**
     * @return 递归
     */
    public int maxDepth3(TreeNode root) {
        if(root == null){
            return 0;
        }
        return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
    }

    public int maxDepth2(TreeNode root) {
        int res = 0;
        Deque<TreeNode> queue = new ArrayDeque<>();
        if(root == null) return res;
        queue.offer(root);
        while(!queue.isEmpty()){
            int levelCount = queue.size();
            for(int i = 0;i < levelCount;i++){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
            res++;
        }
        return res;
    }

    /**
     * @return 递归
     */
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftDepth = maxDepth(root.left) + 1;
        int rightDepth = maxDepth(root.right) + 1;
        return Math.max(leftDepth,rightDepth);
    }
}
